package com.smfx.yxt.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.alibaba.fastjson.JSON;
import com.smfx.yxt.common.AdminUser;
import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Configuration;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.Constants;
import com.smfx.yxt.common.GlobalMap;
import com.smfx.yxt.common.GlobalValue;
import com.smfx.yxt.model.ClassTeachers;
import com.smfx.yxt.model.SysDictData;
import com.smfx.yxt.service.ISysDictDataService;
import com.smfx.yxt.service.ITermInfoService;
import com.smfx.yxt.utils.RequestUtil;
import com.smfx.yxt.view.GradeClassView;
import com.smfx.yxt.view.TermView;
import com.smfx.yxt.wechat.model.WechatUserInfo;
import com.smfx.yxt.wechat.utils.WechatGlobalMap;
import com.smfx.yxt.wechat.utils.WechatGlobalValue;

/**
 * 基础controller
 * @author huqimeng
 * @date 2017年5月8日
 */
public class BaseController {
	
	@Autowired
	protected HttpSession session;
	@Autowired
	protected HttpServletRequest request;
	
	@Autowired
	private ITermInfoService termInfoService;
	@Autowired
	private ISysDictDataService sysDictDataService;
	
	@InitBinder 
    public void initBinder(WebDataBinder binder){
        //日期处理
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
    }
	
	@ModelAttribute("config")
	protected Configuration configInstance() {
		return Configuration.getInstance();
	}
	
	@ModelAttribute("user")
	protected AppUser appUser() {
		return getAppUser();
	}
	
	@ModelAttribute("sysConf")
	protected String sysConf() {
		GlobalValue value = GlobalMap.get(Constants.SYS_CONF);
		if(value == null) {
			List<SysDictData> list = sysDictDataService.selectDictData("sys_conf");
			GlobalMap.put(Constants.SYS_CONF, list);
			return JSON.toJSONString(list);
		}
		return JSON.toJSONString(value.getValue());
	}
	
	@ModelAttribute("term")
	protected TermView getCurrentTerm() {
		WechatGlobalValue value = WechatGlobalMap.get(Const.CACHE_TERM);
		if(value == null) {
			TermView view = termInfoService.selectCurrentTerm();
			view.init();
			WechatGlobalMap.put(Const.CACHE_TERM, view, 24*60*60);
			return view;
		}
		return (TermView) value.getValue();
	}
	
	public String getOpenid() {
		return RequestUtil.getOpenid(request);
	}
	
	public WechatUserInfo getWechatUserInfo() {
		WechatUserInfo userInfo = (WechatUserInfo) session.getAttribute(Constants.WECHAT_USERINFO);
		return userInfo;
	}
	
	public AppUser getAppUser() {
		AppUser appUser = (AppUser) session.getAttribute(Constants.APP_USER);
		return appUser;
	}
	
	public AdminUser getAdminUser() {
		return (AdminUser) request.getSession().getAttribute(Constants.ADMIN_USER);
	}
	
	public static List<GradeClassView> getGradeClassList(List<ClassTeachers> list) {
		List<GradeClassView> viewList = new ArrayList<>();
		Map<String, GradeClassView> map = new HashMap<>();
		for(ClassTeachers ct : list) {
			String gradeKey = String.valueOf(ct.getGrade()) + ct.getStudyType();
			if(!map.containsKey(gradeKey)) {
				GradeClassView view = new GradeClassView();
				List<ClassTeachers> list2 = new ArrayList<>();
				list2.add(ct);
				view.setStudyType(ct.getStudyType());
				view.setGrade(ct.getGrade());
				view.setClassList(list2);
				map.put(gradeKey, view);
				viewList.add(map.get(gradeKey));
				continue;
			}
			((GradeClassView)map.get(gradeKey)).getClassList().add(ct);
		}
		map = null;
		return viewList;
	}
}
